package com.vitas.coin.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.Nullable;

import com.vitas.coin.data.bean.DownloadedBean;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class DownDao extends MyBaseDao {

    String TABLE_NAME = "down";

    @Override
    protected String getTableName() {
        return TABLE_NAME;
    }

    public void insert(final DownloadedBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                SQLiteDatabase db = null;
                try {
                    db = getDataBase();
                    if (db == null) {
                        if (listener != null) {
                            listener.onResult(false, "", null);
                        }
                        return;
                    }
                    ContentValues values = new ContentValues();
                    values.put("uid", bean.id);
                    values.put("path", bean.path);
                    values.put("time", System.currentTimeMillis());
                    bean.dbId = db.insert(TABLE_NAME, null, values);
                    if (listener != null) {
                        listener.onResult(true, null, bean);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    if (listener != null) {
                        listener.onResult(false, e.getMessage(), null);
                    }
                } finally {
                    if (db != null) {
                        closeDataBase();
                    }
                }
            }
        });
    }

    public void update(final DownloadedBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                SQLiteDatabase db = null;
                try {
                    db = getDataBase();
                    if (db == null) {
                        if (listener != null) {
                            listener.onResult(false, "", null);
                        }
                        return;
                    }
                    ContentValues values = new ContentValues();
                    values.put("uid", bean.id);
                    values.put("path", bean.path);
                    values.put("time", System.currentTimeMillis());
                    db.update(TABLE_NAME, values, "id=?", new String[]{bean.dbId + ""});
                    if (listener != null) {
                        listener.onResult(true, null, bean);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    if (listener != null) {
                        listener.onResult(false, e.getMessage(), null);
                    }
                } finally {
                    if (db != null) {
                        closeDataBase();
                    }
                }
            }
        });
    }

    public List<DownloadedBean> getAll() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return Collections.emptyList();
            }
            cursor = db.rawQuery("select * from " + TABLE_NAME + " order by time desc", null);
            if (cursor == null || cursor.getCount() <= 0) {
                return Collections.emptyList();
            }
            List<DownloadedBean> list = new LinkedList<>();
            while (cursor.moveToNext()) {
                DownloadedBean item = new DownloadedBean();
                item.path = cursor.getString(cursor.getColumnIndexOrThrow("path"));
                item.id = cursor.getInt(cursor.getColumnIndexOrThrow("uid"));
                item.dbId = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                list.add(item);
            }
            return list;
        } catch (Throwable e) {
            return Collections.emptyList();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }

    @Nullable
    public DownloadedBean get(int id) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return null;
            }
            cursor = db.rawQuery("select * from " + TABLE_NAME + " where uid=" + id, null);
            if (cursor == null || cursor.getCount() <= 0) {
                return null;
            }
            while (cursor.moveToNext()) {
                DownloadedBean item = new DownloadedBean();
                item.path = cursor.getString(cursor.getColumnIndexOrThrow("path"));
                item.id = cursor.getInt(cursor.getColumnIndexOrThrow("uid"));
                item.dbId = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                if (new File(item.path).exists()) {
                    return item;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
        return null;
    }

    public void getAll(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                List<DownloadedBean> list = getAll();
                if (listener != null) {
                    listener.onResult(true, null, list);
                }
            }
        });
    }
}
